class ZCL_FORMPRINT_SRV_01 definition
  public
  create public .

public section.

  interfaces IF_HTTP_SERVICE_EXTENSION .
protected section.
private section.
ENDCLASS.



CLASS ZCL_FORMPRINT_SRV_01 IMPLEMENTATION.


  method IF_HTTP_SERVICE_EXTENSION~HANDLE_REQUEST.
  DATA: BEGIN OF ls_input,
            Order_Uuid   TYPE c LENGTH 40,
          END OF ls_input.


    DATA(lv_text) = request->get_text( ).

    /ui2/cl_json=>deserialize(
     EXPORTING
       json             = lv_text
     CHANGING
       data             = ls_input
   ).

    TRY.

        DATA(lo_fdp_util) = cl_fp_fdp_services=>get_instance( 'ZSD_ONLINE_SHOP_01' ).

        DATA(lv_schema) = lo_fdp_util->get_xsd(  ).

        DATA(lv_schema_xml) =  cl_abap_conv_codepage=>create_in( )->convert( lv_schema ).

        DATA(lt_keys)  = lo_fdp_util->get_keys( ).

        lt_keys[ name = 'ORDER_UUID' ]-value = ls_input-Order_Uuid.

        DATA(lv_xml) = lo_fdp_util->read_to_xml( lt_keys ).

        DATA(lv_xml_out) =  cl_abap_conv_codepage=>create_in( )->convert( lv_xml ).

        DATA lv_string TYPE string.

        SELECT SINGLE * FROM zformtemplate_01
                        WHERE formid = 'ZDEMOTEST'
                        INTO @DATA(ls_form).

        cl_fp_ads_util=>render_4_pq(
          EXPORTING
            iv_locale       = 'en_US'
            iv_pq_name      = 'DEVTEST_01' "<= Name of the print queue where result should be stored
            iv_xml_data     = lv_xml
            iv_xdp_layout   = ls_form-form_binary
            is_options      = VALUE #(
              trace_level = 4 "Use 0 in production environment
            )
          IMPORTING
            ev_trace_string = DATA(lv_trace)
            ev_pdl          = DATA(lv_pdf)
        ).

        DATA docname TYPE c LENGTH 120.
        docname = 'OnlieShopOrder_' && ls_input-Order_Uuid.

        cl_print_queue_utils=>create_queue_item_by_data(
            "Name of the print queue where result should be stored
            iv_qname = 'DEVTEST_01'
            iv_print_data = lv_pdf
            iv_name_of_main_doc = docname
        ).


      CATCH cx_root INTO DATA(lv_1).
        DATA(lv_error) = lv_1->get_longtext( ).
    ENDTRY.

  endmethod.
ENDCLASS.
